#include "widgets/plotting/XYPlot.h"
#include <QtCore/QVector>
#include <QtGui/QPen>
#include <QtGui/QColor>
#include <cstdio>
#include <cstring>

namespace spectrum {

XYPlot::XYPlot() : QwtPlotCurve()
{
    setPlotStyle(DOT);
}

XYPlot::~XYPlot()
{
}


void XYPlot::setData(const unsigned n, const double * y)
{
    if (n < 1 || y == NULL)
    {
        fprintf(stderr, "ERROR: ScatterPlot::setData(): Invalid data!\n");
        return;
    }

    _y.resize(n);
    memcpy(_y.data(), y, n * sizeof(double));

    _x.resize(n);
    for (unsigned i = 0; i < n; ++i)
    {
        _x[i] = (double)i;
    }

    setRawSamples(_x.data(), _y.data(), n);
}


// Setup the scatter plot style.
// note: The QwtSymbol is managed (i.e. deleted by the QwtPlotCurve object.
void XYPlot::setPlotStyle(const Style style)
{
    switch (style)
    {
        case DOT:
        {
            setSymbol(new QwtSymbol(QwtSymbol::Ellipse, Qt::blue, QPen(Qt::blue),
                        QSize(3, 3)) );
            setPen(QColor(Qt::red));
            setStyle(QwtPlotCurve::NoCurve);
            break;
        }
        case LINE_DOT:
        {
            setSymbol(new QwtSymbol(QwtSymbol::Ellipse, Qt::blue, QPen(Qt::blue),
                        QSize(3, 3)) );
            setStyle(QwtPlotCurve::Lines);
            setCurveAttribute(QwtPlotCurve::Fitted);
            break;
        }
        case LINE:
        {
            setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush, QPen(Qt::black),
                    QSize(5, 5)) );
            setStyle(QwtPlotCurve::Lines);
            setCurveAttribute(QwtPlotCurve::Fitted);
            break;
        }
    };

    setRenderHint(QwtPlotItem::RenderAntialiased);
}

} // namespace spectrum
